package com.hnqcgczy.shop.db;



import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.sql.Connection;
import java.sql.SQLException;

public class Mytransaction {
    private Connection connection;
    ComboPooledDataSource  comboPooledDataSource=new ComboPooledDataSource();

    public Mytransaction(boolean isAutoCommit) {
        try {
            // IDb db = new Dbimpl();//
            connection = comboPooledDataSource.getConnection();
            connection.setAutoCommit(isAutoCommit);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public Connection getConnection() throws SQLException {
        return connection;
    }

    public void closeconn()throws SQLException{
        comboPooledDataSource.close();
    }

    public void commit() {
        try {
            connection.commit();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public void rollback() {
        if (connection != null) {
            try {
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    public static void  main(String[]args){
        Mytransaction mytransaction=new Mytransaction(true);
        try {
            System.out.println(mytransaction.getConnection().isClosed());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
